package com.droidblackbox.manager;

import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class C2DMManager {

	public static final String INTENT_REGISTER;
	public static final String INTENT_REGISTRATION;
	public static final String INTENT_UNREGISTER;
	public static final String INTENT_RECEIVE;

	public static final String KEY_PUSH_NUMBER;
	public static final String KEY_PUSH_SUBMESSAGE;
	public static final String KEY_PUSH_1;
	public static final String KEY_PUSH_2;
	public static final String KEY_PUSH_3;
	public static final String KEY_PUSH_4;
	public static final String KEY_PUSH_5;

	public static final String EXTRA_KEY;

	public static final int ID_RECEIVE_NOTIFICATION;

	static {
		INTENT_REGISTER = "com.google.android.c2dm.intent.REGISTER";
		INTENT_REGISTRATION = "com.google.android.c2dm.intent.REGISTRATION";
		INTENT_UNREGISTER = "com.google.android.c2dm.intent.UNREGISTER";
		INTENT_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";

		KEY_PUSH_NUMBER = KeyValues.number.toString();
		KEY_PUSH_SUBMESSAGE = KeyValues.submessage.toString();
		KEY_PUSH_1 = KeyValues.key1.toString();
		KEY_PUSH_2 = KeyValues.key2.toString();
		KEY_PUSH_3 = KeyValues.key3.toString();
		KEY_PUSH_4 = KeyValues.key4.toString();
		KEY_PUSH_5 = KeyValues.key5.toString();

		EXTRA_KEY = "droidblackbox_receive_message";

		ID_RECEIVE_NOTIFICATION = 951847623;
	}

	private enum KeyValues {
		number, submessage, key1, key2, key3, key4, key5
	}

	private static C2DMManager instance = null;

	private String number;
	private String registration;

	public synchronized static C2DMManager getInstance() {
		if (instance == null) {
			synchronized (C2DMManager.class) {
				if (instance == null) {
					instance = new C2DMManager();
				}
			}
		}

		return instance;
	}

	private C2DMManager() {

	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public void setRegistration(String registration) {
		this.registration = registration;
	}

	public String getRegistration() {
		return registration;
	}

	public static void push(String number, Map<String, String> message) {
		try {

			JSONObject jsonObject = new JSONObject();
			Iterator<String> iterator = message.keySet().iterator();
			while (iterator.hasNext()) {
				final String key = (String) iterator.next();
				final String value = message.get(key);

				try {
					jsonObject.put(key, value);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			WASManager.add(WASManager.KEY_HEADER, WASManager.HEADER_PARTNER);
			WASManager.add(WASManager.KEY_BODY, WASManager.BODY_INSERT);
			WASManager.add(WASManager.KEY_NUMBER, number);
			WASManager.add(WASManager.KEY_MESSAGE, jsonObject.toString());

			WASManager.conn();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
